/*
 * jsSO - Javascript Shared Objects
 * http://pro-web.at/projects/jsso
 *
 * Copyright (c) 2009 Daniel Prieler
 * Date: 2009-02-24
 * Version: 0.1
 */

(function($){

window.jsSO = {
	_debug: false,
	_data: {},
	_updates: {},

	connect: function(objectName, serverUrl) {
		this.debug('connect');

		var flashvars = {
			objectName: objectName,
			serverUrl: serverUrl
			// persisent: true
		};
		var params = {};
		var attributes = {};

		$(function(){
			$('body').append('<div style="position: absolute; left: -1000px; top: -1000px;"><div id="jsSOSwf"></div></div>');
			swfobject.embedSWF('swf/connector.swf', "jsSOSwf", this._debug ? 320 : 1, this._debug ? 140 : 1, "9.0.0", false, flashvars, params, attributes);
		});
	},
	flashInit: function() {
		this.debug('flashInit');
	},
	onConnect: function() {
		this.debug('onConnect');
	},
	debug: function(text) {
		if (this._debug && window.console && window.console.log) {
			window.console.log(text);
		}
	},
	_swf: function() {
		return $('#jsSOSwf')[0];
	},
	sync: function(updates, data) {
		this.debug('sync');
		//this.debug(updates);
		//this.debug(data);
		this._data = data;
		this._updates = updates;
		
		// trigger onSync events
		$(this).trigger('sync.jsSO', [updates, data]);
	},
	onSync: function(func) {
		// bind new onSync event
		$(this).bind('sync.jsSO', func);
	},

	// set a data item
	set: function(item, data) {
		this._swf().set(item, data);
	},

	// data and updates
	getData: function() {
		return this.data;
	},
	getUpdates: function() {
		return this.updates;
	}
};

})(jQuery);
